const Charts = require('./wxcharts.js');

/**
 * 参数说明
opts                         Object
opts.canvasId                String required                    微信小程序canvas-id
opts.width                   Number required                canvas宽度，单位为px
opts.height                  Number required                canvas高度，单位为px
opts.title                   Object           (only for ring chart)
opts.title.name              String           标题内容
opts.title.fontSize          Number            标题字体大小（可选，单位为px）
opts.title.color             String           标题颜色（可选）
opts.subtitle                Object         (only for ring chart)
opts.subtitle.name           String           副标题内容
opts.subtitle.fontSize       Number          副标题字体大小（可选，单位为px）
opts.subtitle.color          String          副标题颜色（可选）
opts.animation               Boolean default true         是否动画展示
opts.legend                  Boolen default true       是否显示图表下方各类别的标识
opts.type                    String required 图表类型，可选值为pie, line, column, area……
opts.categories              Array required       (饼图、圆环图不需要) 数据类别分类
opts.dataLabel               Boolean default true     是否在图表中显示数据内容值
opts.dataPointShape          Boolean default true   是否在图表中显示数据点图形标识
opts.xAxis                   Object       X轴配置
opts.xAxis.disableGrid       Boolean default false      不绘制X轴网格
opts.yAxis                   Object    Y轴配置
opts.yAxis.format            Function           自定义Y轴文案显示
opts.yAxis.min               Number        Y轴起始值
opts.yAxis.max               Number           Y轴终止值
opts.yAxis.title             String       Y轴title
opts.yAxis.disabled          Boolean default false        不绘制Y轴
opts.series                  Array required        数据列表====>>>>>>

==========>>>>>>>>>
数据列表每项结构定义
dataItem                      Object
dataItem.data                 Array required (饼图、圆环图为Number) 数据
dataItem.color                String 例如#7cb5ec 不传入则使用系统默认配色方案
dataItem.name                 String 数据名称
dateItem.format               Function 自定义显示数据内容

 */
function buildPie(data) {
  /**
   * data eg: 
   [{
      name: '成交量1',
      data: 15,
    }, {
      name: '成交量2',
      data: 35,
    }, {
      name: '成交量3',
      data: 78,
    }, {
      name: '成交量4',
      data: 63,
    }, {
      name: '成交量2',
      data: 35,
    }, {
      name: '成交量3',
      data: 78,
    }, {
      name: '成交量4',
      data: 63,
    }, {
      name: '成交量2',
      data: 35,
    }, {
      name: '成交量3',
      data: 78,
    }, {
      name: '成交量3',
      data: 78,
    }]
   */
  var windowWidth = 320;
  try {
    var res = wx.getSystemInfoSync();
    windowWidth = res.windowWidth;
  } catch (e) {
    console.error('getSystemInfoSync failed!');
  }
  new Charts({
    animation: true,
    canvasId: 'pieCanvas',
    type: 'pie',
    series: data,
    width: windowWidth,
    height: 300,
    dataLabel: true,
  });
}

function createSimulationData() {
  var categories = [];
  var data = [];
  for (var i = 0; i < 10; i++) {
    categories.push('2016-' + (i + 1));
    data.push(Math.random() * (20 - 10) + 10);
  }
  // data[4] = null;
  return {
    categories: categories,
    data: data
  }
}

function createSimulationData2() {
  var categories = [];
  var data = [];
  for (var i = 0; i < 12; i++) {
    categories.push('2019-' + (i + 1));
    data.push(Math.random(10) + 60);
  }
  // data[4] = null;
  return {
    categories: categories,
    data: data
  }
}

function buildLine(data) {
  // data = createSimulationData2();
  var windowWidth = 320;
  try {
    var res = wx.getSystemInfoSync();
    windowWidth = res.windowWidth;
  } catch (e) {
    console.error('getSystemInfoSync failed!');
  }
  new Charts({
    canvasId: 'lineCanvas',
    type: 'line',
    categories: data.categories,
    animation: true,
    legend: false,
    background: '#f5f5f5',
    series: [
      // X轴数据
      {
        // name: '体重',
        data: data.data,
        // format: function (val, name) {
        //   return val.toFixed(1) + 'kg';
        // }
      }, 
      // Y轴数据
      // {
      //   name: '成交量2',
      //   data: [2, 0, 0, 3, null, 4, 0, 0, 2, 0],
      //   format: function (val, name) {
      //     return val.toFixed(2) + '万';
      //   }
      // }
    ],
    xAxis: {
      disableGrid: true
    },
    yAxis: {
      // title: 'title',
      format: function (val) {
        return val.toFixed(1);
      },
    },
    width: windowWidth,
    height: 200,
    dataLabel: false,
    dataPointShape: true,
    extra: {
      lineStyle: 'curve'
    }
  });
}

function buildColumn(data) {
  var windowWidth = 320;
  try {
    var res = wx.getSystemInfoSync();
    windowWidth = res.windowWidth;
  } catch (e) {
    console.error('getSystemInfoSync failed!');
  }
  new Charts({
    canvasId: 'columnCanvas',
    type: 'column',
    animation: true,
    categories: chartData.main.categories,
    series: [{
      name: '成交量',
      data: chartData.main.data,
      format: function (val, name) {
        return val.toFixed(2) + '万';
      }
    }],
    yAxis: {
      format: function (val) {
        return val + '万';
      },
      title: 'hello',
      min: 0
    },
    xAxis: {
      disableGrid: false,
      type: 'calibration'
    },
    extra: {
      column: {
        width: 15
      }
    },
    width: windowWidth,
    height: 200,
  });
  
}

function buildArea(data) {
  var windowWidth = 320;
  try {
    var res = wx.getSystemInfoSync();
    windowWidth = res.windowWidth;
  } catch (e) {
    console.error('getSystemInfoSync failed!');
  }
  new Charts({
    canvasId: 'areaCanvas',
    type: 'area',
    categories: ['1', '2', '3', '4', '5', '6'],
    animation: true,
    series: [{
      name: '成交量1',
      data: [32, 45, 0, 56, 33, 34],
      format: function (val) {
        return val.toFixed(2) + '万';
      }
    }, {
      name: '成交量2',
      data: [15, 20, 45, 37, 4, 80],
      format: function (val) {
        return val.toFixed(2) + '万';
      },
    }],
    yAxis: {
      title: '成交金额 (万元)',
      format: function (val) {
        return val.toFixed(2);
      },
      min: 0,
      fontColor: '#8085e9',
      gridColor: '#8085e9',
      titleFontColor: '#f7a35c'
    },
    xAxis: {
      fontColor: '#7cb5ec',
      gridColor: '#7cb5ec'
    },
    extra: {
      legendTextColor: '#cb2431'
    },
    width: windowWidth,
    height: 200
  });
  
}

function buildRing(data) {
  var windowWidth = 320;
  try {
    var res = wx.getSystemInfoSync();
    windowWidth = res.windowWidth;
  } catch (e) {
    console.error('getSystemInfoSync failed!');
  }
  new Charts({
    animation: true,
    canvasId: 'ringCanvas',
    type: 'ring',
    extra: {
      ringWidth: 25,
      pie: {
        offsetAngle: -45
      }
    },
    title: {
      name: '70%',
      color: '#7cb5ec',
      fontSize: 25
    },
    subtitle: {
      name: '收益率',
      color: '#666666',
      fontSize: 15
    },
    series: [{
      name: '成交量1',
      data: 15,
      stroke: false
    }, {
      name: '成交量2',
      data: 35,
      stroke: false
    }, {
      name: '成交量3',
      data: 78,
      stroke: false
    }, {
      name: '成交量4',
      data: 63,
      stroke: false
    }],
    disablePieStroke: true,
    width: windowWidth,
    height: 200,
    dataLabel: false,
    legend: false,
    padding: 0
  });
  
}

module.exports = {
  buildPie: buildPie,
  buildLine: buildLine,
  buildColumn: buildColumn,
  buildArea: buildArea,
  buildRing: buildRing,
}